package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Group;
import com.example.demo.service.GroupService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/group")
public class GroupController {
    @Resource
    GroupService groupService;

    //新增
    @PostMapping
    public Result<?> save(@RequestBody Group group) {
        if (null != groupService.selectByUsername(group.getGroupName())) {
            return Result.error("-1", "商品类型已存在,请重新输入");
        } else {

            groupService.insert(group);
            return Result.success();
        }
    }


    //修改
    @PutMapping
    public Result<?> update(@RequestBody Group group) {
        groupService.updateById(group);
        return Result.success();
    }

    //删除
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        groupService.deleteById(id);
        return Result.success();
    }

    //分页查询
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "100000") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        Page<Group> userPage = groupService.selectPage(pageNum, pageSize, search);
        return Result.success(userPage);
    }

    @GetMapping("/selectAll")
    public Result<?> selectAll(Group group) {
        List<Group> groupList = groupService.selectAll(group);
        return Result.success(groupList);
    }

}
